import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller.dart';

/// 全屏头像查看器
/// 
/// 🎭 动画策略：
/// - 进入时：配合原始头像的Hero标签产生飞行动画
/// - 退出时：使用自然的页面淡出动画（无Hero包装）
/// - 这样实现了不对称动画效果：进入炫酷，退出自然
class FullScreenAvatarViewer extends StatelessWidget {
  final String imageUrl;
  final bool isNetworkImage;
  final MyIndexController controller;

  const FullScreenAvatarViewer({
    super.key,
    required this.imageUrl,
    required this.isNetworkImage,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Column(
        children: [
          // 顶部关闭按钮区域
          SafeArea(
            bottom: false,
            child: Row(
              children: [
                IconButton(
                  onPressed: () => Get.back(),
                  icon: const Icon(
                    Icons.close,
                    color: Colors.white,
                    size: 28,
                  ),
                ),
              ],
            ),
          ),
          
          // 头像显示区域 - 顶部对齐，在关闭按钮下面，宽度占满屏幕
          // 添加点击空白处关闭功能
          Expanded(
            child: GestureDetector(
              // 点击空白区域关闭页面
              onTap: () => Get.back(),
              child: Container(
                width: double.infinity,
                alignment: Alignment.topCenter,
                color: Colors.transparent, // 设置透明色确保可以接收点击事件
                child: InteractiveViewer(
                  minScale: 0.5,
                  maxScale: 3.0,
                  child: isNetworkImage
                      ? Image.network(
                          imageUrl,
                          fit: BoxFit.fitWidth,
                          width: double.infinity,
                          errorBuilder: (context, error, stackTrace) => _buildDefaultAvatar(),
                          loadingBuilder: (context, child, loadingProgress) {
                            if (loadingProgress == null) return child;
                            return Center(
                              child: CircularProgressIndicator(
                                value: loadingProgress.expectedTotalBytes != null
                                    ? loadingProgress.cumulativeBytesLoaded /
                                        loadingProgress.expectedTotalBytes!
                                    : null,
                                color: Colors.white,
                              ),
                            );
                          },
                        )
                      : _buildDefaultAvatar(),
                ),
              ),
            ),
          ),
          
          // 底部按钮区域
          SafeArea(
            top: false,
            child: Container(
              padding: const EdgeInsets.all(16),
              child: Column(
                children: [
                  // 提示文字
                  const Text(
                    '双指可缩放图片 • 点击空白处返回',
                    style: TextStyle(
                      color: Colors.white70,
                      fontSize: 14,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 16),
                  
                  // 按钮列 - 三个按钮垂直排列
                  Column(
                    children: [
                      // 更换头像按钮
                      SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: ElevatedButton.icon(
                          onPressed: () async {
                            await controller.onChangeAvatarTap();
                            Get.back(); // 关闭当前页面
                          },
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.blue.shade600,
                            foregroundColor: Colors.white,
                            elevation: 2,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          icon: const Icon(Icons.photo_camera, size: 20),
                          label: const Text(
                            '更换头像',
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                      
                      const SizedBox(height: 12),
                      
                      // 保存头像按钮
                      SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: ElevatedButton.icon(
                          onPressed: controller.onSaveAvatarTap,
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.green.shade600,
                            foregroundColor: Colors.white,
                            elevation: 2,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          icon: const Icon(Icons.download, size: 20),
                          label: const Text(
                            '保存头像',
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                      
                      const SizedBox(height: 12),
                      
                      // 查看闪忆码按钮
                      SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: ElevatedButton.icon(
                          onPressed: controller.onViewFlashCodeTap,
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.purple.shade600,
                            foregroundColor: Colors.white,
                            elevation: 2,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          icon: const Icon(Icons.qr_code, size: 20),
                          label: const Text(
                            '查看闪忆码',
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
  
  /// 构建默认头像
  Widget _buildDefaultAvatar() {
    return Container(
      width: 200,
      height: 200,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: RadialGradient(
          colors: [
            Colors.grey.shade300,
            Colors.grey.shade500,
          ],
        ),
      ),
      child: const Icon(
        Icons.person,
        size: 100,
        color: Colors.white,
      ),
    );
  }
}
